<!-- 护理等级列表页 -->
<template>
  <div class="min-h serveGrade bg-wt">
    <!-- 组件整合，其他组件需要定义在这个div内部 -->
    <SearchForm
      :searchData="pagination"
      @handleSearch="getList"
      @handleReset="handleReset"
      @handleClear="handleClear"
    ></SearchForm>
    <DialogForm
      ref="formRef"
      :visible="visible"
      :title="title"
      :data="formBaseData"
      :planData="planData"
      @handleClose="handleClose"
      @handleAdd="handleAdd"
      @handleEditForm="handleEditForm"
    ></DialogForm>
    <TableList
      :listData="listData"
      :pagination="pagination"
      :total="total"
      :loading="dataLoading"
      @getCurrent="getCurrent"
      @handleEdit="handleEdit"
      @handleBulid="handleBulid"
      @handleClickDelete="handleDelete"
      @handleForbidden="handleForbidden"
    ></TableList>
    <!--禁用弹层-->
    <Forbidden
      :visible="forbiddenVisible"
      :text="operateText"
      @handleClose="handleCloseForbidden"
      @handleSubmit="handleSubmitForbidden"
    />
    <!-- 删除弹层 -->
    <Delete
      :visible="deleteVisible"
      :deleteText="operateText"
      @handleClose="handleCloseDelete"
      @handleDelete="handleSubmitDelete"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'
import Forbidden from '@/components/Forbidden/index.vue'
// 删除弹层
import Delete from '@/components/OperateDialog/index.vue'
import {
  getLevelDetails,
  getLevelList,
  getnursAllPlanList,
  levelAdd,
  levelDelete,
  levelStatus,
  levelUpdate
} from '@/api/serve'
import TableList from './components/TableList.vue'
import SearchForm from './components/SearchForm.vue'
import DialogForm from './components/DialogFrom.vue'

const listData = ref([])
const total = ref(0)
const dataLoading = ref(false)
const pagination = ref({
  pageNum: 1,
  pageSize: 10
})
onMounted(() => {
  getList()
  getAllPlan()
})
// 点击翻页
const getCurrent = (pageInfo) => {
  pagination.value.pageNum = pageInfo.current
  pagination.value.pageSize = pageInfo.pageSize
  getList()
}
// 分页查询
const getList = async () => {
  const result = await getLevelList(pagination.value)
  if (result.code === 200) {
    listData.value = result.data.records
    total.value = Number(result.data.total)
  }
}
// 搜索重置
const handleReset = () => {
  pagination.value = {
    pageNum: 1,
    pageSize: 10
  }
  getList()
}
// 清空单个搜索条件
const handleClear = (val) => {
  if (val === 'name') {
    delete pagination.value.name
  } else if (val === 'status') {
    delete pagination.value.status
  }
  getList()
}
const visible = ref(false)
const title = ref('')
const formBaseData = ref({})
const planData = ref([])
// 引用dialogForm组件
const formRef = ref({})
// 点击新增按钮，弹窗显示
const handleBulid = () => {
  visible.value = true
  title.value = '新增'
}
// 弹窗关闭
const handleClose = () => {
  visible.value = false
}
// 点击确定新增
const handleAdd = async (val) => {
  const result = await levelAdd(val)
  if (result.code === 200) {
    MessagePlugin.success('新增成功')
    getList()
    handleClose()
    // 重置表单数据
    formRef.value.handleClear()
  } else {
    MessagePlugin.error(result.msg)
  }
}
// 点击编辑按钮，弹窗显示
const handleEdit = (row) => {
  getDetails(row.id)
  title.value = '编辑'
  visible.value = true
}
// 根据id获取详情
const getDetails = async (id) => {
  const result = await getLevelDetails(id)
  if (result.code === 200) {
    formBaseData.value = result.data
  }
}
// 获取所有护理计划
const getAllPlan = async () => {
  const result = await getnursAllPlanList()
  if (result.code === 200) {
    planData.value = result.data
  }
}
// 编辑时，点击确定按钮
const handleEditForm = async (val) => {
  const result = await levelUpdate(val)
  if (result.code === 200) {
    MessagePlugin.success('编辑成功')
    handleClose()
    getList()
    // 重置表单数据
    formRef.value.handleClear()
  } else {
    MessagePlugin.error(result.msg)
  }
}
// 获取操作行id
const typeId = ref('')
// 禁用组件数据
const forbiddenVisible = ref(false)
const typeStatus = ref() // 禁用启用目标值
const operateText = ref('护理等级') // 要操作的内容提示
const statusText = ref('') // 禁用启用提示
// 禁用与启用，显示弹窗
const handleForbidden = (row) => {
  typeId.value = row.id
  if (row.status === 1) {
    forbiddenVisible.value = true
    typeStatus.value = 0 // 如果当前为启用状态，则禁用
    statusText.value = '禁用成功'
  } else {
    typeStatus.value = 1 // 如果当前为禁用状态，则启用
    handleSubmitForbidden() // 相当于直接点了禁用弹窗的确定
    statusText.value = '启用成功'
  }
}
// 禁用与启用，点击确定按钮
const handleSubmitForbidden = async () => {
  const params = {
    id: typeId.value,
    status: typeStatus.value
  }
  const result = await levelStatus(params)
  if (result.code === 200) {
    handleCloseForbidden()
    MessagePlugin.success(statusText.value)
    getList()
  } else {
    MessagePlugin.error(result.msg)
  }
}
// 禁用与启用，点击取消按钮
const handleCloseForbidden = () => {
  forbiddenVisible.value = false
}
// 删除弹窗
const deleteVisible = ref(false)
const handleDelete = (row) => {
  typeId.value = row.id
  deleteVisible.value = true
}
// 删除弹窗，点击确定按钮
const handleSubmitDelete = async () => {
  const result = await levelDelete(typeId.value)
  if (result.code === 200) {
    MessagePlugin.success('删除成功')
    handleCloseDelete()
    getList()
  } else {
    MessagePlugin.error(result.msg)
  }
}
// 删除弹窗，点击取消按钮
const handleCloseDelete = () => {
  deleteVisible.value = false
}
</script>
